package edu.whut.mall.common.controller;

import edu.whut.mall.common.api.CommonResult;
import edu.whut.mall.common.service.IFdfsService;
import edu.whut.mall.common.vo.FileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Wayne
 * @projectName d9lab-mall
 * @description: 通用文件处理控制器
 * @date 2020/10/12 15:25
 */
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/file")
@RestController
@Api(value="文件管理器",tags = {"文件管理器"})
public class FileController {

    private final IFdfsService fdfsService;

    @ApiOperation("用户批量上传文件")
    @PostMapping(value = "/uploadFiles")
    public CommonResult uploadFiles(MultipartFile[] files, HttpServletRequest request) throws Exception {
        List<FileVo> res = new ArrayList<>(files.length);
        for (MultipartFile file : files) {
            FileVo fileVo = fdfsService.upload(file);
            res.add(fileVo);
        }
        return CommonResult.success(res,"上传文件成功");
    }

    @ApiOperation("用户上传单个文件")
    @PostMapping(value = "/uploadFile")
    public CommonResult uploadFile(MultipartFile file, HttpServletRequest request) throws Exception {
        FileVo fileVo = fdfsService.upload(file);
        return CommonResult.success(fileVo,"上传文件成功");
    }
}
